<template>
  <div>
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="电话">
          <el-input v-model="searchForm.phone" placeholder="请输入电话"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 预约信息表格 -->
    <el-table :data="tableData" border style="width: 100%;">
      <el-table-column prop="id" label="预约ID" width="80"/>
      <el-table-column prop="userRealName" label="用户姓名"/>
      <el-table-column prop="doctorRealName" label="医生姓名"/>
      <el-table-column prop="appointTime" label="预约时间" width="180"/>
      <el-table-column prop="status" label="状态" :formatter="formatStatus"/>
      <el-table-column prop="createTime" label="创建时间" width="180"/>
      <el-table-column prop="updateTime" label="更新时间" width="180"/>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="danger" size="small" @click="handleDeleteAppointment(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import {ref, reactive, onMounted} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {appointmentDeleteOne, appointmentSelectPage} from "@/api/appointment.js";

// 表格数据
const tableData = ref([]);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(10);

// 搜索条件
const searchForm = reactive({
  phone: '',
});

// 获取预约列表
const fetchAppointments = async () => {
  try {
    const params = {
      page: currentPage.value,
      size: pageSize.value,
      phone: searchForm.phone,
    };
    if (params.phone === '') {
      delete params.phone;
    }
    const response = await appointmentSelectPage(params);
    tableData.value = response.data.records;
    total.value = response.data.total;
  } catch (error) {
    ElMessage.error('获取预约列表失败');
  }
};

// 删除预约
const handleDeleteAppointment = (appointmentId) => {
  ElMessageBox.confirm('确认删除该预约?', '提示', {
    type: 'warning',
  }).then(async () => {
    try {
      await appointmentDeleteOne(appointmentId);
      ElMessage.success('删除成功');
      await fetchAppointments();
    } catch (error) {
      ElMessage.error('删除失败');
    }
  });
};

// 分页事件
const handleSizeChange = (size) => {
  pageSize.value = size;
  fetchAppointments();
};

const handleCurrentChange = (page) => {
  currentPage.value = page;
  fetchAppointments();
};

// 搜索
const handleSearch = () => {
  currentPage.value = 1;
  fetchAppointments();
};

// 重置搜索
const resetSearch = () => {
  currentPage.value = 1;
  Object.assign(searchForm, {
    phone: '',
  });
  fetchAppointments();
};

// 状态格式化
const formatStatus = (row) => {
  switch (row.status) {
    case 0:
      return '待确认';
    case 1:
      return '已确认';
    case 2:
      return '已完成';
    case 3:
      return '已取消';
    default:
      return '未知状态';
  }
};

// 初始化数据
onMounted(() => {
  fetchAppointments();
});
</script>

<style scoped>
.search-bar {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 